package com.zuowei.ycy.user.ui.order;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.and.yzy.frame.util.RetrofitUtils;
import com.bigkoo.pickerview.OptionsPickerView;
import com.zuowei.ycy.R;
import com.zuowei.ycy.user.http.Order;
import com.zuowei.ycy.ui.BaseToolbarAty;
import com.zuowei.ycy.user.util.AppJsonUtil;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 订单评价
 */
public class OrderPinjiaAty extends BaseToolbarAty {
    @Bind(R.id.tv_time)
    TextView tv_time;
    @Bind(R.id.ratingbar)
    RatingBar ratingbar;
    @Bind(R.id.edit_content)
    EditText editContent;
    @Bind(R.id.rl_use_time)
    RelativeLayout rlUseTime;

    private OptionsPickerView pvOptions;

    private String delivery_time="30";
    private String order_id;

    @Override
    public int getLayoutId() {
        return R.layout.order_pinjia;
    }

    @Override
    public void initData() {
        mToolbar.setTitle("评价");
        order_id = getIntent().getStringExtra("order_id");
    }

    @Override
    public void requestData() {

    }

    @OnClick({R.id.rl_use_time, R.id.tv_commit})
    @Override
    public void btnClick(View view) {
        switch (view.getId()) {
            case R.id.rl_use_time:

                if (pvOptions == null) {
                    initTime();
                }
                pvOptions.show();
                break;
            case R.id.tv_commit:
                String rating = ratingbar.getRating()+"";
                String content = editContent.getText().toString();

                if (TextUtils.isEmpty(delivery_time)){
                    showToast("请选择配送时间");
                    break;
                }
                if (TextUtils.isEmpty(content)){
                    showToast("请填写评价");
                    break;
                }

                showLoadingDialog("");
                doHttp(RetrofitUtils.createApi(Order.class).commentOrder(order_id, rating, content, delivery_time), 0);
                break;

        }
    }


    ArrayList<String> times;

    private void initTime() {

        pvOptions = new OptionsPickerView(this);
        pvOptions.setTitle("配送用时");

        times = new ArrayList<>();
        times.add("10分钟");
        times.add("20分钟");
        times.add("30分钟");
        times.add("40分钟");
        times.add("50分钟");
        times.add("60分钟");
        times.add("70分钟");


        pvOptions.setPicker(times);
        pvOptions.setCyclic(false);
        pvOptions.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3) {
                delivery_time = times.get(options1).substring(0,2);
                tv_time.setText(times.get(options1));
            }
        });
    }

    @Override
    public void onSuccess(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {
        if (what==0){
            showToast(AppJsonUtil.getResultInfo(result).getMessage());
            finish();
        }
        super.onSuccess(result, call, response, what);
    }
}
